package com.atguigu.aiproject.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 定义用于存放用户创作作品 CreationDetailsVO 的队列名称
    public static final String CREATION_DETAILS_QUEUE_NAME = "creation_details_queue";
    public static final String DIRECT_EXCHANGE_NAME = "creation_direct_exchange";
    public static final String DIRECT_ROUTING_KEY = "creation.details";

    // 邮件队列配置
    public static final String MAIL_QUEUE_NAME = "mail_queue";
    public static final String MAIL_DIRECT_EXCHANGE_NAME = "mail_direct_exchange";
    public static final String MAIL_DIRECT_ROUTING_KEY = "mail.routing.key";

    // 聊天消息队列配置
    public static final String CHAT_QUEUE_NAME = "chat_queue";
    public static final String CHAT_EXCHANGE_NAME = "chat.exchange";
    public static final String CHAT_ROUTING_KEY = "chat.message";

    // TODO 死信队列配置（用于消息确认机制）
    /*public static final String DLX_EXCHANGE_NAME = "dlx_exchange";
    public static final String CREATION_DLX_QUEUE_NAME = "creation_dlx_queue";
    public static final String MAIL_DLX_QUEUE_NAME = "mail_dlx_queue";*/

    @Bean
    public Queue creationDetailsQueue() {
        // durable: true（持久化）, exclusive: false（非独占）, autoDelete: false（不自动删除）
        return new Queue(CREATION_DETAILS_QUEUE_NAME, true);
    }

    /**
     * 创作作品交换机（主题型，更灵活的路由）
     */
    @Bean
    public DirectExchange creationDirectExchange() {
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    /**
     * 绑定创作作品队列到直连交换机
     */
    @Bean
    public Binding creationDirectBinding(Queue creationDetailsQueue, DirectExchange creationDirectExchange) {
        return BindingBuilder.bind(creationDetailsQueue)
                .to(creationDirectExchange)
                .with(DIRECT_ROUTING_KEY);
    }

    @Bean
    public Queue mailQueue() {
        // 邮件队列
        return QueueBuilder.durable(MAIL_QUEUE_NAME)
                // .withArgument("x-dead-letter-exchange", DLX_EXCHANGE_NAME) // 死信交换机
                // .withArgument("x-dead-letter-routing-key", MAIL_DLX_QUEUE_NAME) // 死信路由键
                .build();
    }

    @Bean
    public DirectExchange mailDirectExchange() {
        // 邮件直连交换机
        return ExchangeBuilder.directExchange(MAIL_DIRECT_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    @Bean
    public Binding mailDirectBinding(Queue mailQueue, DirectExchange mailDirectExchange) {
        // 邮件队列绑定邮件直连交换机
        return BindingBuilder.bind(mailQueue)
                .to(mailDirectExchange)
                .with(MAIL_DIRECT_ROUTING_KEY);
    }

    // 聊天消息队列
    @Bean
    public Queue chatQueue() {
        return QueueBuilder.durable(CHAT_QUEUE_NAME).build();
    }

    // 聊天消息交换机
    @Bean
    public DirectExchange chatExchange() {
        return ExchangeBuilder.directExchange(CHAT_EXCHANGE_NAME)
                .durable(true)
                .build();
    }

    // 绑定聊天队列到交换机
    @Bean
    public Binding chatBinding(Queue chatQueue, DirectExchange chatExchange) {
        return BindingBuilder.bind(chatQueue)
                .to(chatExchange)
                .with(CHAT_ROUTING_KEY);
    }

    /**
     * 配置JSON消息转换器
     * 替代默认的JDK序列化，提高性能和兼容性
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}